package com.learn.project2.service.admin.category;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.learn.project2.bean.MarketCategory;
import com.learn.project2.bean.MarketCategoryExample;
import com.learn.project2.bean.MarketGoods;
import com.learn.project2.bean.MarketGoodsExample;
import com.learn.project2.config.customException.NoChooseCategoryL1Exception;
import com.learn.project2.mapper.MarketCategoryMapper;
import com.learn.project2.mapper.MarketGoodsMapper;
import com.learn.project2.model.bo.admin.category.CategoryCreateBO;
import com.learn.project2.model.bo.admin.category.CategoryDeleteBO;
import com.learn.project2.model.bo.admin.category.CategoryUpdateBO;
import com.learn.project2.model.vo.admin.category.CategoryCreateVO;
import com.learn.project2.model.vo.admin.category.CategoryListVO;
import com.learn.project2.model.vo.admin.category.Categoryl1VO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description: TODO 商场管理-商品类目
 * @author 亓永杰
 * @date 2022-01-08 21:17
 * @version 1.0
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    MarketCategoryMapper categoryMapper;

    @Autowired
    MarketGoodsMapper goodsMapper;
/**
 * @description: 查询商品类目
 * @param:
 * @return:  CategoryListVO
 * @author 亓永杰
 * @date: 2022-01-08 21:37
 */
    @Override
    public CategoryListVO listCategory() {
        //设置页码和页面大小
        MarketCategoryExample level1Example = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = level1Example.createCriteria().andDeletedEqualTo(false);
        criteria.andLevelEqualTo("L1");
        long pagesize = categoryMapper.countByExample(level1Example);
        PageHelper.startPage(1, (int) pagesize);
        ArrayList<CategoryListVO.ListBean> level1VOS = new ArrayList<>();
        List<MarketCategory> level1 = categoryMapper.selectByExample(level1Example);

        for (MarketCategory category : level1) {
            CategoryListVO.ListBean level1VO = new CategoryListVO.ListBean();
            level1VO.setDesc(category.getDesc());
            level1VO.setIconUrl(category.getIconUrl());
            level1VO.setId(category.getId());
            level1VO.setKeywords(category.getKeywords());
            level1VO.setLevel(category.getLevel());
            level1VO.setName(category.getName());
            level1VO.setPicUrl(category.getPicUrl());
            MarketCategoryExample level2Example = new MarketCategoryExample();
            level2Example.createCriteria().andPidEqualTo(category.getId()).andDeletedEqualTo(false);

            List<MarketCategory> level2 = categoryMapper.selectByExample(level2Example);
            ArrayList<CategoryListVO.ListBean.ChildrenBean> level2VOS = new ArrayList<>();
            for (MarketCategory category2 : level2) {
                CategoryListVO.ListBean.ChildrenBean level2VO = new CategoryListVO.ListBean.ChildrenBean();
                level2VO.setDesc(category2.getDesc());
                level2VO.setIconUrl(category2.getIconUrl());
                level2VO.setId(category2.getId());
                level2VO.setKeywords(category2.getKeywords());
                level2VO.setLevel(category2.getLevel());
                level2VO.setName(category2.getName());
                level2VO.setPicUrl(category2.getPicUrl());
                level2VOS.add(level2VO);
            }
            level1VO.setChildren(level2VOS);
            level1VOS.add(level1VO);
        }
        CategoryListVO categoryListVO = new CategoryListVO();
        PageInfo<CategoryListVO.ListBean> pageInfo = new PageInfo<>(level1VOS);
        categoryListVO.setLimit(1);
        categoryListVO.setList(pageInfo.getList());
        categoryListVO.setPage((int) pagesize);
        categoryListVO.setPages(pageInfo.getPages());
        categoryListVO.setTotal((int) pageInfo.getTotal());


        return categoryListVO;
    }
/**
 * @description: 获取一级类目
 * @param:
 * @return:
 * @author 亓永杰
 * @date: 2022-01-08 21:38
 */
    @Override
    public Categoryl1VO getL1() {
        MarketCategoryExample l1Example = new MarketCategoryExample();
        l1Example.createCriteria().andLevelEqualTo("L1").andDeletedEqualTo(false);
        List<MarketCategory> l1Categories = categoryMapper.selectByExample(l1Example);
        Categoryl1VO categoryl1VO = new Categoryl1VO();
        PageInfo<MarketCategory> pageInfo = new PageInfo<>(l1Categories);
        List<MarketCategory> list = pageInfo.getList();
        ArrayList<Categoryl1VO.ListBean> l1VOS = new ArrayList<>();
        for (MarketCategory category : list) {
            Categoryl1VO.ListBean l1VO = new Categoryl1VO.ListBean();
            l1VO.setValue(category.getId());
            l1VO.setLabel(category.getName());
            l1VOS.add(l1VO);
        }
        categoryl1VO.setLimit(l1Categories.size());
        categoryl1VO.setList(l1VOS);
        categoryl1VO.setPage(1);
        categoryl1VO.setPages(pageInfo.getPages());
        categoryl1VO.setTotal((int) pageInfo.getTotal());
        return categoryl1VO;
    }
/**
 * @description: 添加商品类目
 * @param:  CategoryCreateBO
 * @return:  CategoryCreateVO
 * @author 亓永杰
 * @date: 2022-01-08 22:26
 */
    @Override
    public CategoryCreateVO insertCategory(CategoryCreateBO categoryCreateBO) throws NoChooseCategoryL1Exception {
        MarketCategory category = new MarketCategory();
        if (categoryCreateBO.getPid() == 0 && "L2".equals(categoryCreateBO.getLevel())){
            throw new NoChooseCategoryL1Exception();
        }
        category.setDesc(categoryCreateBO.getDesc());
        category.setAddTime(new Date());
        category.setUpdateTime(new Date());
        category.setIconUrl(categoryCreateBO.getIconUrl());
        category.setKeywords(categoryCreateBO.getKeywords());
        category.setLevel(categoryCreateBO.getLevel());
        category.setName(categoryCreateBO.getName());
        category.setPicUrl(categoryCreateBO.getPicUrl());
        category.setPid(categoryCreateBO.getPid());
        int i = categoryMapper.insertSelective(category);
        return new CategoryCreateVO(category.getId(),category.getName(),category.getKeywords(),category.getDesc(),category.getPid(),category.getIconUrl(),category.getPicUrl(),category.getLevel(),category.getAddTime(),category.getUpdateTime());
    }
/**
 * @description: 编辑类目
 * @param:
 * @return:
 * @author 亓永杰
 * @date: 2022-01-08 22:50
 */
    @Override
    public void updateCategory(CategoryUpdateBO categoryUpdateBO) {
        MarketCategory category = new MarketCategory();
        category.setPid(categoryUpdateBO.getPid());
        category.setId(categoryUpdateBO.getId());
        category.setPicUrl(categoryUpdateBO.getPicUrl());
        category.setName(categoryUpdateBO.getName());
        category.setLevel(categoryUpdateBO.getLevel());
        category.setKeywords(categoryUpdateBO.getKeywords());
        category.setIconUrl(categoryUpdateBO.getIconUrl());
        category.setUpdateTime(new Date());
        category.setDesc(categoryUpdateBO.getDesc());
        int i = categoryMapper.updateByPrimaryKeySelective(category);

    }
/**
 * @description: 删除类目(逻辑删除):删除类目下的所有商品
 * @param:
 * @return:
 * @author 亓永杰
 * @date: 2022-01-08 23:15
 */
    @Override
    public void deleteCategory(CategoryDeleteBO categoryDeleteBO) {
        //一级类目
        MarketCategory l1 = new MarketCategory();
        l1.setDeleted(true);
        l1.setId(categoryDeleteBO.getId());
        categoryMapper.updateByPrimaryKeySelective(l1);
        //删除一级类目所对应的二级类目
        if (categoryDeleteBO.getChildren() != null){
            List<CategoryDeleteBO.ListBean> l2BOS = categoryDeleteBO.getChildren();
            for (CategoryDeleteBO.ListBean l2BO : l2BOS) {
                MarketCategory l2 = new MarketCategory();
                l2.setId(l2BO.getId());
                l2.setDeleted(true);
                categoryMapper.updateByPrimaryKeySelective(l2);
                MarketGoods goods = new MarketGoods();
                goods.setDeleted(true);
                MarketGoodsExample goodsExample = new MarketGoodsExample();
                goodsExample.createCriteria().andCategoryIdEqualTo(l2.getId());
                goodsMapper.updateByExampleSelective(goods,goodsExample);
            }
        }
    }


}
